package com.multi.thread.chapter07.demo06;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Format02 {
	
	private static class DateTools {
		private static ThreadLocal<SimpleDateFormat> tl = new ThreadLocal<>();
		
		public static SimpleDateFormat getSimpleDateFormat(String pattern) {
			SimpleDateFormat sdf = tl.get();
			if (sdf == null) {
				sdf = new SimpleDateFormat(pattern);
				tl.set(sdf);
			}
			return sdf;
		}
	}
	
	private static class MyThread extends Thread {
		private String date;
		
		public MyThread(String date) {
			super();
			this.date = date;
		}
		
		@Override
		public void run() {
			try {
				Date parseDate = DateTools.getSimpleDateFormat("yyyy-MM-dd").parse(date);
				String newDateString = DateTools.getSimpleDateFormat("yyyy-MM-dd").format(parseDate);
				if (!newDateString.equals(date)) {
					System.out.println("ThreadName = " + Thread.currentThread().getName() + "报错了, 日期字符串  = "
							+ date + ", 转换成的日期 = " + newDateString);
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static void main(String[] args) {
		String[] dataStringArray = new String[]{"2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04",
				"2000-01-05", "2000-01-06", "2000-01-07", "2000-01-08", "2000-01-09", "2000-01-10"};
		MyThread[] threadArray = new MyThread[10];
		for (int i = 0; i < 10; i++) {
			threadArray[i] = new MyThread(dataStringArray[i]);
		}
		for (int i = 0; i < 10; i++) {
			threadArray[i].start();
		}
	}

}
